package Stream;

import Conversion.Parent;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TestStream {
    public static void main(String[] args) {
        List<Integer> integers = Arrays.asList(1, 3, 2, 5, 4, 7, 6, 9, 8);
        List<Integer> collect = integers.stream().filter(integer -> integer > 3).collect(Collectors.toList());
        System.out.println(collect);
        List<Integer> collect1 = integers.stream().sorted().collect(Collectors.toList());
        System.out.println("collect1 = " + collect1);
    }

    @Test
    public void test1(){
        List<Integer> integers = Arrays.asList(1, 2, 3);
        List<String> list = Arrays.asList("一", "二", "三");

        System.out.println(list.size());
        String [] s= {"一","二"};
        List<String> list1 = Arrays.asList(s);
        Iterator<String> iterator = list1.iterator();
        while (iterator.hasNext()){
            System.out.println( iterator.next());
        }

        int [] a ={9,10};
        List ints = Arrays.asList(a);
        System.out.println(ints.get(0));
        int i = ((int[]) ints.get(0))[0];
        System.out.println(i);
        List list2 = CollectionUtils.arrayToList(a);
        System.out.println(list2);
        List<Integer> collect2 = Arrays.stream(a).boxed().collect(Collectors.toList());
        System.out.println(collect2);
        Integer [] b ={1,2};
        List<Integer> integers1 = Arrays.asList(b);
        List<Integer> collect = Stream.of(b).collect(Collectors.toList());
        List<Integer> collect1 = Arrays.stream(b).collect(Collectors.toList());
        System.out.println(integers.get(0));
    }
    @Test
    public void test2() {
        int[] a = {1, 2, 3};
        Arrays.stream(a).forEach(System.out::println);
        System.out.println(Arrays.asList(a).get(0));
    }
    @Test
    public void test3(){
        Parent parentA = null;

        Parent parentB = Parent.builder().age("12")
                .name("张三")
                .salary(new BigDecimal(12))
                .sex("男").build();

        Parent parentC = new Parent();

        Optional<String> optionalAge = Optional.ofNullable(parentA).map(Parent::getAge);
        optionalAge.ifPresent(o->{
            parentC.setAge(optionalAge.get());
        });
        System.out.println(parentC);

        Optional<Parent> optionalParent = Optional.ofNullable(parentB);
        optionalParent.ifPresent(op->{
            BeanUtils.copyProperties(optionalParent.get(),parentC);
        });
        System.out.println(parentC);
    }
    @Test
    public void test4(){
        List<Integer>list =new ArrayList<>();
        list.add(1);
        list.add(3);
        list.add(4);
        list.add(2);
        list.add(6);
        list.add(9);
        list.add(8);
        list.add(7);
        list.add(5);
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
        Integer max = Collections.max(list);
        System.out.println(max);
        Integer min = Collections.min(list);
        System.out.println(min);
        Double collect = list.stream().collect(Collectors.averagingDouble(Integer::doubleValue));//计算平均数
        System.out.println(collect);
        Long collect1 = list.stream().collect(Collectors.counting());//计算元素个数
        System.out.println(collect1);
        Integer collect2 = list.stream().collect(Collectors.summingInt(Integer::intValue));//计算和
        System.out.println(collect2);
        list.stream().collect(Collectors.maxBy(Integer::compareTo)).ifPresent(l-> System.out.println(l));
        list.stream().collect(Collectors.minBy(Integer::compareTo)).ifPresent(l-> System.out.println(l));


    }
}
